package com.wang.system.manager;


import com.wang.common.core.constants.CacheConstants;
import com.wang.common.redis.service.RedisService;
import com.wang.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {

    @Autowired
    private RedisService redisService;

    /**
     * 把竞赛id列表和竞赛基本信息都存入缓存 (以便c端访问)
     * @param exam 竞赛基本信息
     */
    public void addCache(Exam exam){

        Long examId = exam.getExamId(); //获取竞赛id

        //存入竞赛id
        redisService.leftPushForList(getUnfinishedExamListKey(),examId);
        //存入竞赛基本信息
        redisService.setCacheObject(getDetailKey(examId),exam);
    }

    /**
     * 删除指定id的缓存
     * @param examId 竞赛id
     */
    public void deleteCache(Long examId){
        //删除竞赛id
        redisService.removeForList(getUnfinishedExamListKey(), examId);
        //删除竞赛基本信息
        redisService.deleteObject(getDetailKey(examId));
        //删除竞赛题目id列表
        redisService.deleteObject(getExamQuestionListKey(examId));
    }

    private String getUnfinishedExamListKey() {
        //获取未完赛竞赛id的key --> exam:unfinished:list
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    private String getDetailKey(Long examId) {
        //传入竞赛id,获取竞赛详情的key
        //竞赛详情的key --> "exam:detail:" + examId
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getExamQuestionListKey(Long examId) {
        //传入竞赛id,获取竞赛题目id列表的key
        //竞赛题目id列表的key --> "exam:question:list:" + examId
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }
}
